﻿using YNTK.Common.Const;
using YNTK.Dependency;
using YNTK.System.Entitys.Permission;
using YNTK.System.Interfaces.Permission;
using YNTK.System.Interfaces.System;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using Shipeng.Util;
using YNTK.System.Entitys.Dto.Menu;
using System.Collections.Generic;
using YNTK.System.Entitys.Dto.Permission.User;

namespace YNTK.Common.Core.Manager
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public class UserManager : IUserManager, IScoped
    {
        private readonly ITkUsersService _tkUserService; // 用户服务

        private readonly ISysCacheService _sysCacheService;

        private readonly HttpContext _httpContext;

        /// <summary>
        /// 用户信息
        /// </summary>
        public UserEntity User
        {
            get => _tkUserService.GetUserInfoById(UserId);
        }

        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserId
        {
            get => _httpContext.User.FindFirst(ClaimConst.CLAINM_USERID)?.Value;
        }

        /// <summary>
        /// 组织id
        /// </summary>
        public string OrganizeId
        {
            get => _httpContext.User.FindFirst(ClaimConst.ORGANIZEID)?.Value;
        }

        /// <summary>
        /// 机构id
        /// </summary>
        public string MechanismId
        {
            get => _httpContext.User.FindFirst(ClaimConst.MECHANISMID)?.Value;
        }

        /// <summary>
        /// 角色id
        /// </summary>
        public string RoleId
        {
            get => _httpContext.User.FindFirst(ClaimConst.ROLEID)?.Value;
        }

        /// <summary>
        /// 用户账号
        /// </summary>
        public string Account
        {
            get => _httpContext.User.FindFirst(ClaimConst.CLAINM_ACCOUNT)?.Value;
        }

        /// <summary>
        /// 用户昵称
        /// </summary>
        public string RealName
        {
            get => _httpContext.User.FindFirst(ClaimConst.CLAINM_REALNAME)?.Value;
        }

        /// <summary>
        /// 当前用户 token
        /// </summary>
        public string ToKen
        {
            get => _httpContext.Request.Headers["Authorization"];
        }

        public string TenantId
        {
            get => _httpContext.User.FindFirst(ClaimConst.TENANT_ID)?.Value;
        }

        /// <summary>
        /// 是否是管理员
        /// </summary>
        public bool IsAdministrator
        {
            get => _httpContext.User.FindFirst(ClaimConst.CLAINM_ADMINISTRATOR)?.Value == ((int)Enum.AccountType.Administrator).ToString();
        }

        /// <summary>
        /// 账号类型【0.平台运营人员 1.超级管理员 2.组织管理员 3.组织用户 4.第三方登录用户 5.游客用户】
        /// </summary>
        public int AccountType
        {
            get => (int)_httpContext.User.FindFirst(ClaimConst.ACCOUNTTYPE)?.Value.ToInt();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_tkUserService"></param>
        /// <param name="sysCacheService"></param>
        /// <param name="httpContextAccessor"></param>
        public UserManager(ITkUsersService tkUserService, ISysCacheService sysCacheService)
        {
            _tkUserService = tkUserService;
            _sysCacheService = sysCacheService;
            _httpContext = App.HttpContext;
        }

        /// <summary>
        /// 获取用户登录信息
        /// </summary>
        /// <returns></returns>
        public async Task<TkUserInfoDto> GetUserInfo()
        {
            var user = new TkUserInfoDto();
            var userCache = await _sysCacheService.GetUserInfo(TenantId + "_" + UserId);
            if (userCache == null) 
            {
                user = (await _tkUserService.Get(UserId)).data;
                user.userId = user.id;
                user.isAdministrator = user.accountType == 1;
                if (!string.IsNullOrWhiteSpace(user.roleId)) user.roleIds = new string[] { user.roleId };
            }
            else user = userCache;
            return user;
        }

        /// <summary>
        /// 获取用户菜单树架构数据
        /// </summary>
        public async Task<List<MenuTreeDto>> GetMenuData()
        {
            var menuData= await _sysCacheService.GetAsync<List<MenuTreeDto>>($"{TenantId}_{UserId}_menucCache");
            return menuData;
        }

    }
}
